<template>
  <div id="SeatMange">
    <div class="title">
      <h1>座位列表</h1>
      <el-button type="primary" @click="toOrder" style="margin-right: 70px"
        >座位预约情况</el-button
      >
    </div>
    <div class="seat">
      <span>自习室总座位：{{ total }} </span>
      <span>当前使用：{{ length }} </span>
      <span>当前空闲:{{ total - length }} </span>
    </div>
    <div class="search">
      <el-form
        ref="form"
        :model="form"
        label-width="80px"
        :inline="true"
        style="padding: 20px 20px 0 20px"
      >
        <el-form-item label="区域">
          <el-select v-model="form.areaId" placeholder="请选择区域">
            <el-option
              v-for="v in area"
              :key="v.id"
              :label="v.name"
              :value="v.name"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="form.state" placeholder="请选择状态">
            <el-option label="空闲" value="0"></el-option>
            <el-option label="使用中" value="1"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div>
        <el-button type="info" style="margin-right: 10px" @click="search"
          >查询
        </el-button>
        <el-button type="info" style="margin-right: 100px" @click="rest"
          >重置
        </el-button>
      </div>
    </div>
    <el-table
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%"
      @select="handleSelectionChange"
    >
      <el-table-column type="selection" label="全选 "> </el-table-column>
      <el-table-column prop="areaName" label="区域" align="center">
      </el-table-column>
      <el-table-column prop="num" label="桌子号码" align="center">
        <template slot-scope="scope">{{ scope.row.price + "号座位" }}</template>
      </el-table-column>
      <el-table-column prop="state" label="状态" align="center">
        <template slot-scope="scope">{{
          scope.row.state == 0
            ? "空闲"
            : scope.row.state == 1
            ? "使用中"
            : "---"
        }}</template>
      </el-table-column>
      <el-table-column prop="price" label="价格" align="center">
        <template slot-scope="scope">{{ scope.row.price + "/h" }}</template>
      </el-table-column>
      <el-table-column
        prop="light"
        label="当前电源开关"
        text-align="center"
        align="center"
      >
        <template slot-scope="scope">{{
          scope.row.light == 0 ? "关闭" : scope.row.light == 1 ? "开启" : "---"
        }}</template>
      </el-table-column>
      <el-table-column label="电源控制" prop="light" align="center">
        <template slot-scope="scope">
          <el-button
            :type="
              scope.row.light == 0
                ? 'success'
                : scope.row.light == 1
                ? 'danger'
                : 'info'
            "
            @click="toSwitch(scope.row)"
            >{{
              scope.row.light == 0
                ? "点击开灯"
                : scope.row.light == 1
                ? "点击关灯"
                : "无法识别"
            }}</el-button
          >
          <!-- <el-switch
            v-model="scope.row.light"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-value="1"
            inactive-value="0"
            @change="toSwitch(scope.row)"
          >
          </el-switch> -->
          <!-- {{
          scope.row.light == 0 ? "关闭" : scope.row.light == 1 ? "开启" : "---"
        }} -->
        </template>
      </el-table-column>
    </el-table>
    <div class="page">
      <el-button type="danger" size="mini" @click="updateClose"
        >批量关灯</el-button
      >
      <el-button type="success" size="mini" @click="updateOpen"
        >批量开灯</el-button
      >
      <el-pagination
        style="float: right"
        @current-change="handleCurrentChange"
        :current-page.sync="page"
        :page-size="limit"
        layout="prev, pager, next"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      form: {
        areaId: "",
        state: "",
      },
      tableData: [
        // 0空闲  1使用中  0关灯 1开灯
      ],
      page: 1,
      limit: 4,
      total: 0,
      //批量选择
      select: [],
      arr: [],
      //区域
      area: [],
      //使用中的个数
      length: 0,
    };
  },
  methods: {
    toOrder: function () {
      this.$router.push("/home/SeatOrder");
    },
    handleCurrentChange: function (val) {
      this.page = val;
      this.getData();
    },
    //渲染数据
    getData: function () {
      this.$axios({
        url: "/seat/queryall",
        method: "post",
        data: {
          page: this.page,
          limit: this.limit,
          state: this.form.state,
          areaName: this.form.areaId,
        },
      }).then(({ data }) => {
        this.total = data.data.count;
        this.tableData = data.data.Seats;
        //挑出使用中座位个数
        let arr = [...data.data.Seats];
        for (let i in arr) {
          if (arr[i].state == 0) {
            arr.splice(i, 1);
          }
        }
        this.length = arr.length;
      });
    },
    //开关灯
    toSwitch: function (row) {
      if (row.light == 0) {
        this.$axios({
          url: "/seat/updatelight",
          method: "put",
          data: [
            {
              id: row.id,
              light: 1,
            },
          ],
        }).then(({ data }) => {
          console.log(data);
          if (data.code == 0) {
            this.$message.success("开灯成功！");
            this.getData();
          } else {
            this.$message.error(data.msg);
          }
        });
      } else if (row.light == 1) {
        this.$axios({
          url: "/seat/updatelight",
          method: "put",
          data: [
            {
              id: row.id,
              light: 0,
            },
          ],
        }).then(({ data }) => {
          if (data.code == 0) {
            this.$message.error("关灯成功！");
            this.getData();
          } else {
            this.$message.error(data.msg);
          }
        });
      }
    },
    handleSelectionChange(val) {
      this.select = val;
    },
    //批量开灯
    updateOpen: function () {
      for (let i in this.select) {
        delete this.select[i].areaName;
        delete this.select[i].idList;
        delete this.select[i].limit;
        delete this.select[i].num;
        delete this.select[i].page;
        delete this.select[i].price;
        delete this.select[i].state;
        this.select[i].light =1;
      }
      console.log(this.select);

      this.$axios({
        url: "/seat/updatelight",
        method: "put",
        data: this.select,
      }).then(({ data }) => {
        if (data.code == 0) {
          this.$message.success("批量关灯成功！");
          this.getData();
          this.select = [];
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    //批量关灯
    updateClose: function () {
      for (let i in this.select) {
        delete this.select[i].areaName;
        delete this.select[i].idList;
        delete this.select[i].limit;
        delete this.select[i].num;
        delete this.select[i].page;
        delete this.select[i].price;
        delete this.select[i].state;
        this.select[i].light = 0;
      }
      console.log(this.select);

      this.$axios({
        url: "/seat/updatelight",
        method: "put",
        data: this.select,
      }).then(({ data }) => {
        if (data.code == 0) {
          this.$message.success("批量开灯成功！");
          this.getData();
          this.select = [];
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    //获取区域下拉
    toArea: function () {
      this.$axios({
        url: "/area/queryall",
      }).then(({ data }) => {
        this.area = data.data;
      });
    },
    search: function () {
      this.getData();
    },
    rest: function () {
      this.form.areaId = "";
      this.form.state = "";
      this.getData();
    },
  },
  mounted: function () {
    this.getData();
    this.toArea();
  },
};
</script>
<style lang="less" scoped>
#SeatMange {
  padding: 0;
  text-align: left;
  line-height: 0;
  .title {
    display: flex;
    justify-content: space-between;
  }
  .seat {
    height: 30px;
    margin-top: 10px;
    display: flex;
    align-items: center;
    span {
      margin-right: 20px;
    }
  }
  .search {
    margin-top: 10px;
    width: 100%;
    background: #d7d7d7;
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
  .page {
    margin-top: 20px;
  }
}
</style>
